//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.WhsTrans4KListController');
	com.yonyou.ncmm_pac_pda.WhsTrans4KListController = function() {
		com.yonyou.ncmm_pac_pda.WhsTrans4KListController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//    
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$evaljs(js) {
		eval(js)
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$viewPage0_onload(sender, args) {

	}

	function downloadBillData(condition) {
		//获取默认组织，如果默认组织为空，提示
		var stororg = $stringToJSON($cache.read("defaultstockorg"));
		if(stororg == null || stororg == "") {
			$toast("下载前请先进行业务设置！");
			return;
		}
		condition["pk_org"] = "='" + stororg.pk_stockorg + "'";
		var incondition=null;
		if(NCMMPubUtil.isEmpty(condition["cwarehouseid"])&&NCMMPubUtil.isEmpty(condition["cwarehouseid"])){
		
		var stordoc = $stringToJSON($cache.read("defaultstordoc"));
		if(stordoc != null && stordoc != "") {
		incondition=JSON.parse(JSON.stringify(condition));;
		condition["cwarehouseid"] ="='"+stordoc.pk_stordoc+"'";
		incondition["cotherwhid"]="='"+stordoc.pk_stordoc+"'";
		}
		}
		$js.showLoadingBar();
		//测试连接
		NCMMDBUtil.testNCConnection(function(data1) {
			if(data1 == false || data1 == "false") {
				$js.hideLoadingBar();
				$toast("下载失败请检查网络设置！");
				return;
			} else {
				//下载最新的VO数据
				queryDataByConditon(condition,incondition, function(data,indata) {	
					if(data==null&&indata==null){
					    $js.hideLoadingBar();
					    $toast("没有获取到符合条件的订单！");
					    return;
					}	    
					var vocount=0;		
					var insertvos=[];
					var updatevos=[];	
					var billinfos=[];
					if(data != null) {
						//获取本地旧数据，没有扫描数量清空	
						if( NCMMPubUtil.isNotEmpty(data.billinfos)&&data.billinfos.length > 0){				
	                    //vocount=vocount+NCMMDBUtil.getBillCount(data);
						pushToBillInfoArray( billinfos,data.billinfos);	
						}	
									}
					if(indata != null) {
						//获取本地旧数据，没有扫描数量清空
						if( NCMMPubUtil.isNotEmpty(indata.billinfos)&&indata.billinfos.length > 0){				
	                    //vocount=vocount+NCMMDBUtil.getBillCount(indata);
						pushToBillInfoArray( billinfos,indata.billinfos);
						}
					}
					if(NCMMPubUtil.isNotEmpty(billinfos)&&billinfos.length > 0) {	
				    var oldData = getOldData();
							for(var i = 0; i < billinfos.length; i++) {
								//插入表
								var items = billinfos[i].billinfo.items;
								vocount=vocount+items.length;
								for(var j = 0; j < items.length; j++) {
									var billdata = $.extend({}, items[j].item, billinfos[i].billinfo.head);
									if(!NCMMPubUtil.isEmpty(oldData) && !NCMMPubUtil.isEmpty(oldData[billdata.cspecialbid])) {
										updatevos.push(billdata);
									} else {
										insertvos.push(billdata);
									}
								}
							}
							if(NCMMPubUtil.isNotEmpty(insertvos)){
								WhsTransUtil.batchInsertTableVOs(WhsTransVO,insertvos);
							}
							if(NCMMPubUtil.isNotEmpty(updatevos)){
								WhsTransUtil.updateBatchVOs(WhsTransVO,updatevos,WhsTransVO.NSCANEDNUM.name);
								WhsTransUtil.updateScans(updatevos);
							}										
							loadListData();
							$js.hideLoadingBar();
							$toast("转库单下载了【" + vocount + "】条数据。");
						} else {
							$js.hideLoadingBar();
							$toast("没有获取到符合条件的订单！");
						}
					 
				});
			}
		});
	}
	function pushToBillInfoArray(billinfos,datainfos){
	     for(var i = 0; i < datainfos.length; i++) {
	         billinfos.push(datainfos[i]);
	  }
	}
	function queryDataByConditon(condition,incondition,callback){
	//下载最新的VO数据
		NCMMDBUtil.loadNCBaseData(WhsTransVO, WhsTransVO, NCMMHttpUtil.BILL, condition, function(data) {	
		  if(NCMMPubUtil.isEmpty(incondition)){
		     if(callback != null && typeof(callback) == 'function') {
				callback(data,null);
			 }
			 return;
          }
          NCMMDBUtil.loadNCBaseData(WhsTransVO, WhsTransVO, NCMMHttpUtil.BILL, incondition, function(indata) {
             if(callback != null && typeof(callback) == 'function') {
				callback(data,indata);
			 }
          });
       });
	}
	//获取本地数据，如果没有扫描数量，直接删除，有，返回json格式【行id，vo】
	function getOldData() {
		var returnData = {};
		NCMMPubUtil.queryVOByConditions(WhsTransVO, null, function(data) {
			if(data != null) {
				var datas = eval(data);
				datas = NCMMPubUtil.htmlspecialjson2UI(datas);
				for(var i = 0; i < datas.length; i++) {
					if(datas[i].nscanednum > 0) {
						returnData[datas[i].cspecialbid] = datas[i];
					} else {
						//删除
						NCMMPubUtil.deleteVO(WhsTransVO, datas[i]);
					}
				}
			}
		});
		return returnData;
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$getDataToday(sender, args) {
		var nowdate = NCMMPubUtil.getNowDate();
		var begindate = nowdate + " 00:00:00";
		var enddate = nowdate + " 23:59:59";
		var dbilldate = " between '" + begindate + "' and '" + enddate + "'";
		//加载当天数据
		var condition = {
			"dbilldate": dbilldate
		};
		downloadBillData(condition);
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$getDataWeek(sender, args) {
		//加载本周数据
		var daydate = NCMMDateUtil.getWeekDate();
		var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
		var condition = {
			"dbilldate": dbilldate
		};
		downloadBillData(condition);

	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$getDataMonth(sender, args) {
		//加载本月数据
		var daydate = NCMMDateUtil.getMonthDate();
		var dbilldate = "between '" + daydate.beginDate + "' and '" + daydate.endDate + "'";
		var condition = {
			"dbilldate": dbilldate
		};
		downloadBillData(condition);
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$onload(sender, args) {
		// 加载数据的时候判断如果有表则查询，如果不存在数据表则先创建
		NCMMPubUtil.createTable(WhsTransVO);
		loadListData();
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$itemDelete(sender, args) {
		$view.closePop(popDialogInfo);
		var params = {
			"title": "提示",
			"message": "确定删除所选记录吗？",
			"okbuttontitle": "确定",
			"cancelbuttontitle": "取消",
			"style": "ok-cancel",
			"okaction": "doDelete()",
			"cancelaction": "doCancel()"
		};
		$view.openDialog(params);
	}

	function doCancel() {
		$toast("删除操作已取消。");
	}

	//执行清除操作
	function doDelete() {
		var row = $id("listview").get("row");
		row = $stringToJSON(row);
		if(row.nscanednum != 0 && row.nscanednum != "" && row.nscanednum != null) {
			$alert("已扫描数量不为0，不能删除！")
			return;
		}
		NCMMPubUtil.deleteVO(WhsTransVO, row);
		$toast("删除成功。");
		loadListData();
	}
	//加载到货单已有数据
	function loadListData() {
		NCMMPubUtil.queryVOByConditions(WhsTransVO, null, function(data) {		
			var showData = eval(data);
			showData = NCMMPubUtil.htmlspecialjsonarray2UI(showData);
			var bitnumberJson = {};
			for(var i = 0; i < showData.length; i++) {
				var itemData = showData[i];
				if(NCMMPubUtil.isEmpty(bitnumberJson) || NCMMPubUtil.isEmpty(bitnumberJson[itemData["cunitid"]])) {
					var bitnumber = WhsTransUtil.getUnitBitnumber(itemData["cunitid"]);	
					if(!NCMMPubUtil.isEmpty(bitnumber)) {
						bitnumberJson[itemData["cunitid"]] = bitnumber;
					}
				}
				if(NCMMPubUtil.isEmpty(bitnumberJson) || NCMMPubUtil.isEmpty(bitnumberJson[itemData["castunitid"]])) {
					var bitnumber = WhsTransUtil.getUnitBitnumber(itemData["castunitid"]);
					if(!NCMMPubUtil.isEmpty(bitnumber)) {
						bitnumberJson[itemData["castunitid"]] = bitnumber;
					}
				}
				if(!NCMMPubUtil.isEmpty(bitnumberJson) && !NCMMPubUtil.isEmpty(bitnumberJson[itemData["cunitid"]])) {
					showData[i]["nnum"] = parseFloat(showData[i]["nnum"]).toFixed(bitnumberJson[itemData["cunitid"]]);
					if(!NCMMPubUtil.isEmpty(showData[i]["nscanednum"])){
					showData[i]["nscanednum"] = parseFloat(showData[i]["nscanednum"]).toFixed(bitnumberJson[itemData["cunitid"]]);
					}
				}
				if(!NCMMPubUtil.isEmpty(bitnumberJson) && !NCMMPubUtil.isEmpty(bitnumberJson[itemData["castunitid"]])) {
					showData[i]["nastnum"] = parseFloat(showData[i]["nastnum"]).toFixed(bitnumberJson[itemData["castunitid"]]);
					if(!NCMMPubUtil.isEmpty(showData[i]["nscanedastnum"])){
					showData[i]["nscanedastnum"] = parseFloat(showData[i]["nscanedastnum"]).toFixed(bitnumberJson[itemData["castunitid"]]);
					}
				}
			}
			var jsondata = {
				listdata: eval(showData)
			};
			$ctx.push(jsondata);
			$ctx.dataBind();
		});
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$openSearchView(sender, args) {
		// 点击查询按钮弹出查询条件画面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.WhsTrans4KQuery",
			isKeep: "true",
			callback: "doQuery()"
		});

	}

	function doQuery() {
		var cdon = $param.getJSONObject("qcondition");
		var json = {};
		json = cdon;
		//$alert(json);
		downloadBillData(json);
	}
	//扫描
	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$scanBillCode(sender, args) {
		$scanner.open({
			bindfield: "billbarcode",
			callback: "billScanBack()"
		});
	}

	function billScanBack() {
		// 扫条码调到采集页面
		var billbarcode = $ctx.getString("billbarcode");
		if(NCMMPubUtil.isEmpty(billbarcode)){
		return;
		}
		//1 获取单据码，然后解析出单据号信息，从本地数据库中查找该码对应的单据信息信息赋值
		var condition = {
			"vbillcode": billbarcode //json.billcode
		};
		NCMMPubUtil.queryVOByConditions(WhsTransVO, condition, function(data) {
			if(typeof(data) == 'undefined' || data == null || data.length == 0) {
				$toast("未找到单据码信息，请重新下载单据信息");
				return;
			}
			//2 打开扫描记录编辑界面
			var vos = eval(data);
			vos = NCMMPubUtil.htmlspecialjsonarray2UI(vos);
			if(vos != null && vos.length > 0&&NCMMPubUtil.isNotEmpty(vos[0]["vbillcode"])) {
				var billinfo = {};
				var billcode = vos[0]["vbillcode"];
				billinfo = {
					"vbillcode": billcode,
					"cspecialhid": vos[0]["cspecialhid"]
				};
					$view.open({
					viewid: "com.yonyou.ncmm_pac_pda.WhsTrans4KScan",
					isKeep: "true",
					row: billinfo
				});
			} else {
				$toast("未找到单据码信息，请重新下载单据信息");
			}
		});

		//$alert(billcode);

	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showDetail(sender, args) {
		$view.closePop(popDialogInfo);
		var row = $id("listview").get("row");
		//row = $stringToJSON(row);
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.WhsTrans4KDetail", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			"row": row
		});
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$doScan(sender, args) {
		$view.closePop(popDialogInfo);
		var row = $id("listview").get("row");
		//row = $stringToJSON(row);
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.WhsTrans4KScan", //目标页面（首字母大写）全名
			"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
			"row": row
		});
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$itemCancel(sender, args) {
		$view.closePop(popDialogInfo);

	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showScanList(sender, args) {
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.WhsTrans4KScanList", //目标页面（首字母大写）全名
			"isKeep": "true" //打开新页面的同时是否保留当前页面，true为保留，false为不保留

		});
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showMenu(sender, args) {
		WhsTransUtil.openDropDownList("btn_menu");
	}

	//function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$search(sender, args) {
	//	NCMMPubUtil.quickFilter();
	//}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$onresume(sender, args) {
		// 加载数据的时候判断如果有表则查询，如果不存在数据表则先创建
		NCMMPubUtil.createTable(WhsTransVO);
		loadListData();
	}

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$close(sender, args) {
		$view.close();

	}
	var popDialogInfo = {
		"viewid": "panel_btn", //显示的控件的id，通常是一个容器控件div
		"popgravity": "popbottom", //popcenter|poptop|popbottom|popleft|popright,弹出后最终显示的位置
		// istouch : 'false'
	};

	function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showItemButton(sender, args) {
		$view.openPop(popDialogInfo);
	}


function com$yonyou$ncmm_pac_pda$WhsTrans4KListController$inputBillCode(sender, args){
        $ctx.dataCollect();		
		billScanBack();		
		$id("textbox0").set("value", null);
}	com.yonyou.ncmm_pac_pda.WhsTrans4KListController.prototype = {
    inputBillCode : com$yonyou$ncmm_pac_pda$WhsTrans4KListController$inputBillCode,
   	showItemButton: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showItemButton,
		close: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$close,
		onresume: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$onresume,
		//search: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$search,
		showMenu: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showMenu,
		scanBillCode: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$scanBillCode,
		doScan: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$doScan,
		showDetail: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$showDetail,
		itemDelete: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$itemDelete,
		itemCancel: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$itemCancel,
		openSearchView: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$openSearchView,
		onload: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$onload,
		getDataMonth: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$getDataMonth,
		getDataWeek: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$getDataWeek,
		getDataToday: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$getDataToday,
		viewPage0_onload: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$viewPage0_onload,
		initialize: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$WhsTrans4KListController$evaljs
	};
	com.yonyou.ncmm_pac_pda.WhsTrans4KListController.registerClass('com.yonyou.ncmm_pac_pda.WhsTrans4KListController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}